---
title: "Replication File (Data Processing Survey 2, Wave 1): The Limits of Public Support for Fiscal Consolidation: Survey Evidence from Great Britain"
author: "Björn Bremer, Charlotte Cavaille, Lisanne de Blok, Catherine de Vries"
date: "February 3, 2026"
output:
  bookdown::html_document2:
    keep_tex: true
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
knitr::opts_chunk$set(message = FALSE)
knitr::opts_chunk$set(warning = FALSE)
```

```{r, include=FALSE}
rm(list = ls())
set.seed(1234567)

# List of packages needed
packages <- c("tidyverse", "haven"
)

# Function to check if packages are installed and install if needed
package_check <- function(pkg){
  if (!require(pkg, character.only = TRUE)) {
    install.packages(pkg, dependencies = TRUE)
    library(pkg, character.only = TRUE)
  }
}

# Apply function to each package
invisible(sapply(packages, package_check))

# Create directories to save data
if (!dir.exists("data")) {
  dir.create("data")
}
```

```{r, include=FALSE}
## clean the data
debt<- read.csv("Debt & Preferences - June - Wave 2_September 12, 2022_04.50.csv", colClasses = c("character"), sep = ",")
debt <- debt[-c(1:2),] # remove unnecessary columns

# change the variable names
names(debt) <- gsub(x = names(debt), pattern = "\\.", replacement = "") 

# creating dummy for speeders
debt$duration <- as.numeric(debt$Durationinseconds)
debt$speeder <- ifelse(debt$duration<115, 1, 0)
#table(debt$speeder)

# create a measure for wether respondents passed the sound check and the attention check and did not speed
debt$Finished <- as.numeric(debt$Finished)
debt$soundC <- as.numeric(debt$soundC)
debt$AC <- as.numeric(debt$AC)
debt$SC_passed <- ifelse(debt$soundC==3, 1, 0)
debt$AC_passed <- ifelse(debt$AC==4, 1, 0)
debt$check <- ifelse(debt$speeder==0 & debt$soundC==3 & debt$AC==4, 1, 0)

# delete individual observations that are the results of testing  
debt <- debt[which(debt$respid!="" & debt$respid!="TESTGBR765" & debt$respid!="[ID]"),]
```

# Variable coding
```{r variables, include=FALSE}
# having thought about debt
debt$think_debt <- as.numeric(debt$think_debt)
table(debt$think_debt)
debt$think_debt_rec <- ifelse(debt$think_debt==4, "A great deal",
                          ifelse(debt$think_debt==3, "Moderate amount",
                             ifelse(debt$think_debt==2, "A little",
                                    ifelse(debt$think_debt==1, "Never", NA))))
debt$think_debt_rec <- as.factor(debt$think_debt_rec)

# having thought about china
debt$think_china <- as.numeric(debt$think_china)
table(debt$think_china)

# having thought about climate change
debt$think_climate <- as.numeric(debt$think_climate_V2_2)
table(debt$think_climate)

# having thought about illegal immigration
debt$think_immi <- as.numeric(debt$think_immi_V1_3)
table(debt$think_immi)

debt$think_index <- debt$think_china + debt$think_climate + debt$think_immi
table(debt$think_index)
debt$think_index_rec <- ifelse(debt$think_index<=4, "Low", ifelse(debt$think_index>4 & debt$think_index<=8, "Medium", ifelse(debt$think_index>8, "High", NA)))
table(debt$think_index_rec)
```

```{r outcomevariables, include=FALSE}
## recode outcome variables

# concern over debt
debt$debt_concern <- as.numeric(debt$serious_debt)
table(debt$debt_concern)

# concern over china
debt$china_concern <- as.numeric(debt$serious_China)
table(debt$china_concern)

# concern over climate
debt$cc_concern <- as.numeric(debt$serious_CC)
table(debt$cc_concern)

# concern over immigration
debt$immi_concern <- as.numeric(debt$serious_immi)
table(debt$immi_concern)

# policy index
debt$pol1_rec <- ifelse(debt$pol1=="", NA, debt$pol1)
debt$pol2_rec <- ifelse(debt$pol2=="", NA, debt$pol2)
debt$pol3_rec <- ifelse(debt$pol3=="", NA, debt$pol3)
debt$pol1_rec <- as.numeric(debt$pol1_rec)
debt$pol2_rec <- as.numeric(debt$pol2_rec)
debt$pol3_rec <- as.numeric(debt$pol3_rec)

debt$debt_index <- debt$pol1_rec + debt$pol2_rec + debt$pol3_rec

# debt vs. climate change
debt$debt_CC <- debt$prioD_CCa
debt$debt_CC <- ifelse(debt$debt_CC=="", debt$prioD_CCb, debt$debt_CC)
debt$debt_CC <- as.numeric(debt$debt_CC)

debt$debt_CC_rec <- ifelse(debt$debt_CC == 2, 1, 
                          ifelse(debt$debt_CC == 1, 0, NA))

# debt vs. immigration
debt$debt_immi <- debt$prioD_immia
debt$debt_immi <- ifelse(debt$debt_immi=="", debt$prioD_immib, debt$debt_immi)
debt$debt_immi <- as.numeric(debt$debt_immi)

debt$debt_immi_rec <- ifelse(debt$debt_immi == 2, 1, 
                          ifelse(debt$debt_immi == 1, 0, NA))

# debt vs. nhs
debt$debt_nhs <- debt$prioD_NHSa
debt$debt_nhs <- ifelse(debt$debt_nhs=="", debt$prioD_NHSb, debt$debt_nhs)
debt$debt_nhs <- as.numeric(debt$debt_nhs)

debt$debt_nhs_rec <- ifelse(debt$debt_nhs == 2, 1, 
                          ifelse(debt$debt_nhs == 1, 0, NA))

# debt vs. inflation
debt$debt_infl <- debt$prioD_infla
debt$debt_infl <- ifelse(debt$debt_infl=="", debt$prioD_inflb, debt$debt_infl)
debt$debt_infl <- as.numeric(debt$debt_infl)

debt$debt_infl_rec <- ifelse(debt$debt_infl == 2, 1, 
                          ifelse(debt$debt_infl == 1, 0, NA))

debt$debt_post <- debt$debt_concern
debt$china_post <- debt$china_concern

debt <- debt %>%
  mutate(across(c(debt_post, china_post, debt_index, debt_nhs_rec, debt_CC_rec, debt_immi_rec, debt_infl_rec), 
                ~ as.numeric(.)))

debt <- debt %>% 
  mutate(across(c(debt_post, china_post, debt_index, debt_nhs_rec, debt_CC_rec, debt_immi_rec, debt_infl_rec), ~ as.numeric(.))) %>%
  mutate(across(c(debt_post, china_post, debt_index, debt_nhs_rec, debt_CC_rec, debt_immi_rec, debt_infl_rec),
                ~ as.vector(scale(.)), 
                .names = "{.col}_z"))

debt$cert_debt_rec <- ifelse(debt$cert_debt==3, "Serious problem",
                             ifelse(debt$cert_debt==2, "Uncertain",
                                    ifelse(debt$cert_debt==1, "Not a serious problem", NA)))
debt$cert_debt_rec <- as.factor(debt$cert_debt_rec)

# certainty on climate
debt$cert_climate <- debt$cert_climate_V1_2 
debt$cert_climate <- as.numeric(debt$cert_climate)
debt$cert_climate_rec <- ifelse(debt$cert_climate==3, "Serious problem",
                             ifelse(debt$cert_climate==2, "Uncertain",
                                    ifelse(debt$cert_climate==1, "Not a serious problem", NA)))
debt$cert_climate_rec <- as.factor(debt$cert_climate_rec)

# certainty on immi
debt$cert_immi <- debt$cert_immi_V2_3 
debt$cert_immi <- as.numeric(debt$cert_immi)
debt$cert_immi_rec <- ifelse(debt$cert_immi==3, "Serious problem",
                             ifelse(debt$cert_immi==2, "Uncertain",
                                    ifelse(debt$cert_immi==1, "Not a serious problem", NA)))
debt$cert_immi_rec <- as.factor(debt$cert_immi_rec)

# certainty on China
debt$cert_china <- debt$cert_China 
debt$cert_china <- as.numeric(debt$cert_china)
debt$cert_china_rec <- ifelse(debt$cert_china==3, "Serious problem",
                             ifelse(debt$cert_china==2, "Uncertain",
                                    ifelse(debt$cert_china==1, "Not a serious problem", NA)))
debt$cert_china_rec <- as.factor(debt$cert_china_rec)

debt$video_op1 <- as.numeric(debt$video_op1)

```

```{r, include=FALSE}
# cleaning wave 3 
debt_w3 <- debt
rm(debt)

debt_w3 <- debt_w3[which(duplicated(debt_w3$respid)==F),] # remove duplicate ids
debt_w3$check_rec <- ifelse(debt_w3$speeder==0 & debt_w3$soundC==3 & debt_w3$AC==4 & debt_w3$Finished==1, 1, 0)
debt_w3 <- debt_w3[which(debt_w3$check_rec==1),]

debt_w3 <- debt_w3 %>% dplyr::select(respid, debt_post, debt_post_z, debt_index, debt_index_z, debt_CC_rec, debt_CC_rec_z, debt_immi_rec, debt_immi_rec_z, debt_nhs_rec, debt_nhs_rec_z, debt_infl_rec, debt_infl_rec_z, debt_concern, cert_debt_rec, china_post, china_post_z, cert_climate_rec, cert_immi_rec, cert_china_rec, video_op2, timing_videoS_PageSubmit, timing_videoUS_PageSubmit, video_op1, ResponseId)

#add suffix for wave 3 for all columns but response id
debt_w3 <- debt_w3 %>%  rename_all(paste0, "_w3")
debt_w3$respid <- debt_w3$respid_w3
```

# Load QVSR data
```{r qvsr data, include=FALSE}
# load and clean the qvsr data 

qvsr <- read.csv("QVSR Debt project Wave 2 FINAL 08_23.csv", colClasses = c("character"), sep = ",")
qvsr <- qvsr %>% dplyr::select(user.id, QV1, QV2, QV3, QV4, QV5)

# rename the variables
qvsr <- qvsr %>%
    rename(
        ResponseId = user.id,
        qvsr_cc = QV1,
        qvsr_nhs = QV2,
        qvsr_inflation = QV3,
        qvsr_immi = QV4,
        qvsr_debt = QV5
    )

qvsr <- qvsr %>% # z standardization of variables
  mutate(across(c(qvsr_cc, qvsr_nhs, qvsr_inflation, qvsr_immi, qvsr_debt), ~ as.numeric(.))) %>%
  mutate(across(c(qvsr_cc, qvsr_nhs, qvsr_inflation, qvsr_immi, qvsr_debt),
                ~ as.vector(scale(.)), 
                .names = "{.col}_z"))
```

# Load data from W2 and merge with data from W3
```{r, echo=FALSE}
# load w2
load("data/data_processed_w2.RData")

# merging w2 and w3
debt_w2_w3 <- left_join(debt_w2, debt_w3, by="respid")
debt_w2_w3$correct_video <- ifelse(debt_w2_w3$treat_w2==2 & debt_w2_w3$video_op1_w3 == 2, "Correct",
                           ifelse(debt_w2_w3$treat_w2==3 & debt_w2_w3$video_op1_w3 == 1, "Correct",
                                  ifelse(debt_w2_w3$treat_w2==1 & debt_w2_w3$video_op1_w3 == 3, "Correct", 
                                         ifelse(debt_w2_w3$video_op1_w3 == 4, "Cannot remember", "Incorrect"))))

debt_w2_w3 <- debt_w2_w3 %>%
    dplyr::select(-ResponseId) %>%  # remove the w2 version
       rename(ResponseId = ResponseId_w3)

# merging the qvsr data to the debt_w2_w3 data
debt_w2_w3_qvsr <- left_join(debt_w2_w3, qvsr, by="ResponseId")
```

```{r, echo=FALSE}
save(debt_w2_w3, debt_w2_w3_qvsr, 
     file = "data/data_processed_w2_w3.RData")
```
